package com.example.padlock.view.callui;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.padlock.R;
import com.example.padlock.view.callui.KeyBoardEnum.ActionEnum;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnLongClick;

/**
 * Dialog 显示视图
 * 
 * @author LanYan
 * 
 */
@SuppressLint("InflateParams")
public class PayPasswordView implements OnClickListener {

	@ViewInject(R.id.pay_keyboard_del)
	private ImageView del;
	@ViewInject(R.id.pay_keyboard_zero)
	private ImageView zero;
	@ViewInject(R.id.pay_keyboard_one)
	private ImageView one;
	@ViewInject(R.id.pay_keyboard_two)
	private ImageView two;
	@ViewInject(R.id.pay_keyboard_three)
	private ImageView three;
	@ViewInject(R.id.pay_keyboard_four)
	private ImageView four;
	@ViewInject(R.id.pay_keyboard_five)
	private ImageView five;
	@ViewInject(R.id.pay_keyboard_sex)
	private ImageView sex;
	@ViewInject(R.id.pay_keyboard_seven)
	private ImageView seven;
	@ViewInject(R.id.pay_keyboard_eight)
	private ImageView eight;
	@ViewInject(R.id.pay_keyboard_nine)
	private ImageView nine;
	@ViewInject(R.id.pay_cancel)
	private TextView cancel;
	@ViewInject(R.id.pay_sure)
	private TextView sure;
	@ViewInject(R.id.pay_box1)
	private TextView box1;
	@ViewInject(R.id.pay_box2)
	private TextView box2;
	@ViewInject(R.id.pay_box3)
	private TextView box3;
	@ViewInject(R.id.pay_box4)
	private TextView box4;
	@ViewInject(R.id.pay_box5)
	private TextView box5;
	@ViewInject(R.id.pay_box6)
	private TextView box6;
	@ViewInject(R.id.pay_title)
	private TextView title;
	@ViewInject(R.id.pay_content)
	private TextView content;

	private ArrayList<String> mList = new ArrayList<String>();
	private View mView;
	private OnPayListener listener;
	private Context mContext;

	public PayPasswordView(String monney, Context mContext,
			OnPayListener listener) {
		getDecorView(monney, mContext, listener);
	}

	public static PayPasswordView getInstance(String monney, Context mContext,
			OnPayListener listener) {
		return new PayPasswordView(monney, mContext, listener);
	}

	public void getDecorView(String monney, Context mContext,
			OnPayListener listener) {
		this.listener = listener;
		this.mContext = mContext;
		mView = LayoutInflater.from(mContext).inflate(
				R.layout.item_paypassword, null);
		// ViewUtils.inject(this, mView);
		initView();
		content.setText("呼叫房号：");
	}

	private void initView() {
		del = (ImageView) mView.findViewById(R.id.pay_keyboard_del);
		del.setOnClickListener(this);
		zero = (ImageView) mView.findViewById(R.id.pay_keyboard_zero);
		zero.setOnClickListener(this);
		one = (ImageView) mView.findViewById(R.id.pay_keyboard_one);
		one.setOnClickListener(this);
		two = (ImageView) mView.findViewById(R.id.pay_keyboard_two);
		two.setOnClickListener(this);
		three = (ImageView) mView.findViewById(R.id.pay_keyboard_three);
		three.setOnClickListener(this);
		four = (ImageView) mView.findViewById(R.id.pay_keyboard_four);
		four.setOnClickListener(this);
		five = (ImageView) mView.findViewById(R.id.pay_keyboard_five);
		five.setOnClickListener(this);
		sex = (ImageView) mView.findViewById(R.id.pay_keyboard_sex);
		sex.setOnClickListener(this);
		seven = (ImageView) mView.findViewById(R.id.pay_keyboard_seven);
		seven.setOnClickListener(this);
		eight = (ImageView) mView.findViewById(R.id.pay_keyboard_eight);
		eight.setOnClickListener(this);
		nine = (ImageView) mView.findViewById(R.id.pay_keyboard_nine);
		nine.setOnClickListener(this);
		cancel = (TextView) mView.findViewById(R.id.pay_cancel);
		cancel.setOnClickListener(this);
		sure = (TextView) mView.findViewById(R.id.pay_sure);
		sure.setOnClickListener(this);
		content = (TextView) mView.findViewById(R.id.pay_content);
		box1 = (TextView) mView.findViewById(R.id.pay_box1);
		box2 = (TextView) mView.findViewById(R.id.pay_box2);
		box3 = (TextView) mView.findViewById(R.id.pay_box3);
		box4 = (TextView) mView.findViewById(R.id.pay_box4);
		box5 = (TextView) mView.findViewById(R.id.pay_box5);
		box6 = (TextView) mView.findViewById(R.id.pay_box6);
	}

	@OnClick({ R.id.pay_keyboard_del, R.id.pay_keyboard_zero,
			R.id.pay_keyboard_one, R.id.pay_keyboard_two,
			R.id.pay_keyboard_three, R.id.pay_keyboard_four,
			R.id.pay_keyboard_five, R.id.pay_keyboard_sex,
			R.id.pay_keyboard_seven, R.id.pay_keyboard_eight,
			R.id.pay_keyboard_nine, R.id.pay_cancel, R.id.pay_sure })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pay_keyboard_del:
			parseActionType(KeyBoardEnum.del);
			break;
		case R.id.pay_keyboard_zero:
			parseActionType(KeyBoardEnum.zero);
			break;
		case R.id.pay_keyboard_one:
			parseActionType(KeyBoardEnum.one);
			break;
		case R.id.pay_keyboard_two:
			parseActionType(KeyBoardEnum.two);
			break;
		case R.id.pay_keyboard_three:
			parseActionType(KeyBoardEnum.three);
			break;
		case R.id.pay_keyboard_four:
			parseActionType(KeyBoardEnum.four);
			break;
		case R.id.pay_keyboard_five:
			parseActionType(KeyBoardEnum.five);
			break;
		case R.id.pay_keyboard_sex:
			parseActionType(KeyBoardEnum.sex);
			break;
		case R.id.pay_keyboard_seven:
			parseActionType(KeyBoardEnum.seven);
			break;
		case R.id.pay_keyboard_eight:
			parseActionType(KeyBoardEnum.eight);
			break;
		case R.id.pay_keyboard_nine:
			parseActionType(KeyBoardEnum.nine);
			break;
		case R.id.pay_cancel:
			parseActionType(KeyBoardEnum.cancel);
			break;
		case R.id.pay_content:
			break;
		case R.id.pay_sure:
			parseActionType(KeyBoardEnum.sure);
			break;
		default:
			break;
		}
		// if(v==zero){
		// parseActionType(KeyBoardEnum.zero);
		// System.out.println("id:"+0);
		// }else if(v==one){
		// parseActionType(KeyBoardEnum.one);
		// System.out.println("id:"+1);
		// }else if(v==two){
		// parseActionType(KeyBoardEnum.two);
		// System.out.println("id:"+2);
		// }else if(v==three){
		// parseActionType(KeyBoardEnum.three);
		// System.out.println("id:"+3);
		// }else if(v==four){
		// parseActionType(KeyBoardEnum.four);
		// System.out.println("id:"+4);
		// }else if(v==five){
		// parseActionType(KeyBoardEnum.five);
		// System.out.println("id:"+5);
		// }else if(v==sex){
		// parseActionType(KeyBoardEnum.sex);
		// }else if(v==seven){
		// parseActionType(KeyBoardEnum.seven);
		// }else if(v==eight){
		// parseActionType(KeyBoardEnum.eight);
		// }else if(v==nine){
		// parseActionType(KeyBoardEnum.nine);
		// }else if(v==cancel){
		// parseActionType(KeyBoardEnum.cancel);
		// }else if(v==sure){
		// parseActionType(KeyBoardEnum.sure);
		// }else if(v==del){
		// parseActionType(KeyBoardEnum.del);
		// }
	}

	@OnLongClick(R.id.pay_keyboard_del)
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		parseActionType(KeyBoardEnum.longdel);
		return false;
	}

	private void parseActionType(KeyBoardEnum type) {
		// TODO Auto-generated method stub
		System.out.println("size:" + mList.size());
		if (type.getType() == ActionEnum.add) {
			if (mList.size() < 6) {
				mList.add(type.getValue());
				updateUi();
			}
		} else if (type.getType() == ActionEnum.delete) {
			if (mList.size() > 0) {
				mList.remove(mList.get(mList.size() - 1));
				updateUi();
			}
		} else if (type.getType() == ActionEnum.cancel) {
			listener.onCancelPay();
		} else if (type.getType() == ActionEnum.sure) {
			String payValue = "";
			for (int i = 0; i < mList.size(); i++) {
				payValue += mList.get(i);
				listener.onSurePay(payValue);
			}
		} else if (type.getType() == ActionEnum.longClick) {
			mList.clear();
			updateUi();
		}

	}

	private void updateUi() {
		// TODO Auto-generated method stub
		if (mList.size() == 0) {
			box1.setText("");
			box2.setText("");
			box3.setText("");
			box4.setText("");
			box5.setText("");
			box6.setText("");
		} else if (mList.size() == 1) {
			box1.setText(mList.get(0));
			box2.setText("");
			box3.setText("");
			box4.setText("");
			box5.setText("");
			box6.setText("");
		} else if (mList.size() == 2) {
			box1.setText(mList.get(0));
			box2.setText(mList.get(1));
			box3.setText("");
			box4.setText("");
			box5.setText("");
			box6.setText("");
		} else if (mList.size() == 3) {
			box1.setText(mList.get(0));
			box2.setText(mList.get(1));
			box3.setText(mList.get(2));
			box4.setText("");
			box5.setText("");
			box6.setText("");
		} else if (mList.size() == 4) {
			box1.setText(mList.get(0));
			box2.setText(mList.get(1));
			box3.setText(mList.get(2));
			box4.setText(mList.get(3));
			box5.setText("");
			box6.setText("");
		} else if (mList.size() == 5) {
			box1.setText(mList.get(0));
			box2.setText(mList.get(1));
			box3.setText(mList.get(2));
			box4.setText(mList.get(3));
			box5.setText(mList.get(4));
			box6.setText("");
		} else if (mList.size() == 6) {
			box1.setText(mList.get(0));
			box2.setText(mList.get(1));
			box3.setText(mList.get(2));
			box4.setText(mList.get(3));
			box5.setText(mList.get(4));
			box6.setText(mList.get(5));
		}
	}

	public interface OnPayListener {
		void onCancelPay();

		void onSurePay(String password);
	}

	public View getView() {
		return mView;
	}
}
